//
//  TECacheViewController.m
//  TintExample
//
//  Created by Kevin Wu on 2/24/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TECacheViewController.h"

@implementation TECacheViewController

#pragma mark - UIViewController

- (void)loadView {
    [super loadView];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addButtonWithTitle:@"Add disk cache" selector:@selector(addDiskCache:) type:1];
    [self addButtonWithTitle:@"Remove disk cache" selector:@selector(removeDiskCache:) type:1];
    [self addButtonWithTitle:@"Add and remove cache" selector:@selector(addAndRemoveDiskCache:) type:1];
    [self addButtonWithTitle:@"Test disk cache" selector:@selector(testDiskCache:) type:1];
    [self addButtonWithTitle:@"Clear all cache" selector:@selector(clearDiskCache:) type:1];
    
    [self addButtonWithTitle:@"Add memory cache" selector:@selector(addMemoryCache:) type:2];
    [self addButtonWithTitle:@"Remove memory cache" selector:@selector(removeMemoryCache:) type:2];
    [self addButtonWithTitle:@"Test memory cache" selector:@selector(testMemoryCache:) type:2];
    
    _diskCache = [TTDiskCache cacheByType:@"HA"];
    _memoryCache = [TTMemoryCache cacheByType:@"HA"];
}


- (void)addDiskCache:(id)sender {
    NSString *strKey = @"strKey";
    NSString *imageKey = @"imageKey";
    NSString *dataKey = @"dataKey";
    
    
    NSString *strValue = @"abcd";
    [_diskCache setString:strValue forKey:strKey];
    
    UIImage *imageValue = [UIImage imageNamed:@"placeholder_normal.png"];
    [_diskCache setImage:imageValue forKey:imageKey];
    
    NSData *dataValue = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test1.xml" ofType:nil]];
    [_diskCache setData:dataValue forKey:dataKey];
}

- (void)removeDiskCache:(id)sender {
    NSString *strKey = @"strKey";
    NSString *imageKey = @"imageKey";
    NSString *dataKey = @"dataKey";
    
    [_diskCache removeCacheForKey:strKey];
    [_diskCache removeCacheForKey:imageKey];
    [_diskCache removeCacheForKey:dataKey];
}

- (void)addAndRemoveDiskCache:(id)sender {
    
    NSString *strKey = @"strQuickKey";
    NSString *strValue = @"jerk";
    
    for (int i=0; i<10; ++i) {
        [_diskCache setString:strValue forKey:strKey];
        [_diskCache removeCacheForKey:strKey];
    }
    
    NSString *imageKey = @"imageQuickKey";
    UIImage *imageValue = [UIImage imageNamed:@"placeholder_normal.png"];
    
    for (int i=0; i<10; ++i) {
        [_diskCache removeCacheForKey:imageKey];
        [_diskCache setImage:imageValue forKey:imageKey];
    }
    
    NSString *dataKey = @"dataQuickKey";
    NSData *dataValue = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test1.xml" ofType:nil]];
    
    for (int i=0; i<10; ++i) {
        [_diskCache removeCacheForKey:dataKey];
        [_diskCache setData:dataValue forKey:dataKey];
    }
}

- (void)testDiskCache:(id)sender {
    NSString *strKey = @"strKey";
    NSString *imageKey = @"imageKey";
    NSString *dataKey = @"dataKey";
    
    if ([_diskCache hasCacheForKey:strKey]) {
        NSString *strValue = [_diskCache stringForKey:strKey];
        NSLog(@"has string cache: %@", strValue);
    } else {
        NSLog(@"has not string cache");
    }
    
    if ([_diskCache hasCacheForKey:strKey]) {
        UIImage *imageValue = [_diskCache imageForKey:imageKey];
        NSLog(@"has image cache: %@", imageValue);
    } else {
        NSLog(@"has not image cache");
    }
    
    if ([_diskCache hasCacheForKey:strKey]) {
        NSData *dataValue = [_diskCache dataForKey:dataKey];
        NSLog(@"has data cache: %d", [dataValue length]);
    } else {
        NSLog(@"has not data cache");
    }
    
}

- (void)clearDiskCache:(id)sender {
    [_diskCache clearCache];
    
//    NSFileManager *fm = [NSFileManager defaultManager];
//    
//    NSString *path = TTDocumentsPath();
//    
//    NSLog(@"=== file name/relative/absolute ====================================");
//    NSLog(@"%@", [fm fileNamesInFolder:path]);
//    NSLog(@"%@", [fm fileRelativePathsInFolder:path]);
//    NSLog(@"%@", [fm fileAbsolutePathsInFolder:path]);
//    NSLog(@"=== directory name/relative/absolute ===============================");
//    NSLog(@"%@", [fm directoryNamesInFolder:path]);
//    NSLog(@"%@", [fm directoryRelativePathsInFolder:path]);
//    NSLog(@"%@", [fm directoryAbsolutePathsInFolder:path]);
//    NSLog(@"=== item name/relative/absolute ====================================");
//    NSLog(@"%@", [fm itemNamesInFolder:path]);
//    NSLog(@"%@", [fm itemRelativePathsInFolder:path]);
//    NSLog(@"%@", [fm itemAbsolutePathsInFolder:path]);
//    
//    NSLog(@"=== all file name/relative/absolute ================================");
//    NSLog(@"%@", [fm allFileNamesInFolder:path]);
//    NSLog(@"%@", [fm allFileRelativePathsInFolder:path]);
//    NSLog(@"%@", [fm allFileAbsolutePathsInFolder:path]);
//    NSLog(@"=== all directory name/relative/absolute ===========================");
//    NSLog(@"%@", [fm allDirectoryNamesInFolder:path]);
//    NSLog(@"%@", [fm allDirectoryRelativePathsInFolder:path]);
//    NSLog(@"%@", [fm allDirectoryAbsolutePathsInFolder:path]);
//    NSLog(@"=== all item name/relative/absolute ================================");
//    NSLog(@"%@", [fm allItemNamesInFolder:path]);
//    NSLog(@"%@", [fm allItemRelativePathsInFolder:path]);
//    NSLog(@"%@", [fm allItemAbsolutePathsInFolder:path]);
//    
//    NSLog(@"=== jpg file name/relative/absolute ================================");
//    NSLog(@"%@", [fm fileNamesMatchingExtension:@"jpg" inFolder:path]);
//    NSLog(@"%@", [fm fileRelativePathsMatchingExtension:@"jpg" inFolder:path]);
//    NSLog(@"%@", [fm fileAbsolutePathsMatchingExtension:@"jpg" inFolder:path]);
//    NSLog(@"=== all jpg file name/relative/absolute ============================");
//    NSLog(@"%@", [fm allFileNamesMatchingExtension:@"jpg" inFolder:path]);
//    NSLog(@"%@", [fm allFileRelativePathsMatchingExtension:@"jpg" inFolder:path]);
//    NSLog(@"%@", [fm allFileAbsolutePathsMatchingExtension:@"jpg" inFolder:path]);
//    NSLog(@"=== pdf file name/relative/absolute ================================");
//    NSLog(@"%@", [fm fileNamesMatchingExtension:@"pdf" inFolder:path]);
//    NSLog(@"%@", [fm fileRelativePathsMatchingExtension:@"pdf" inFolder:path]);
//    NSLog(@"%@", [fm fileAbsolutePathsMatchingExtension:@"pdf" inFolder:path]);
//    NSLog(@"=== all pdf file name/relative/absolute ============================");
//    NSLog(@"%@", [fm allFileNamesMatchingExtension:@"pdf" inFolder:path]);
//    NSLog(@"%@", [fm allFileRelativePathsMatchingExtension:@"pdf" inFolder:path]);
//    NSLog(@"%@", [fm allFileAbsolutePathsMatchingExtension:@"pdf" inFolder:path]);
}


- (void)addMemoryCache:(id)sender {
    [_memoryCache setString:@"god" forKey:@"hehe"];
}

- (void)removeMemoryCache:(id)sender {
    [_memoryCache removeCacheForKey:@"hehe"];
}

- (void)testMemoryCache:(id)sender {
    NSString *value = [_memoryCache stringForKey:@"hehe"];
    NSLog(@"Cache %@ = %@", @"hehe", value);
}

@end
